<template>
  <el-card>
    <div slot="header">
      <h3>{{ cardTitle }}</h3>
    </div>
    <el-form ref="formRef" :model="formData" :rules="formRules">
      <el-form-item label="标签名称：" prop="name">
        <el-input
          v-model="formData.name"
          placeholder="请输入标签名称"
          maxlength="20"
          show-word-limit
          class="w-60"
        />
      </el-form-item>
      <el-form-item label="父级标签：" prop="parentUuid">
        <el-select v-model="formData.parentUuid" clearable>
          <el-option
            v-for="item in parentLabelOptions"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签介绍：" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          placeholder="请输入标签介绍"
          maxlength="100"
          show-word-limit
          class="w-60"
        />
      </el-form-item>
      <el-row type="flex" justify="center" class="w-60">
        <el-button type="primary" size="mini" :loading="submitLoading" @click="handleSubmit">提交</el-button>
        <el-button size="mini" @click="handleGoBack">返回</el-button>
      </el-row>
    </el-form>
  </el-card>
</template>
<script>
import { isResponseSuccess } from '@/utils/bizUtils';
import {
  labelSearchParentService,
  categoryLabelAddService,
  categoryLabelUpdateService
} from '@/api/basics/categoryManage/categoryLabelManage';
export default {
  data () {
    return {
      formData: {}, // 表单数据
      // 表单校验规则
      formRules: {
        name: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入标签介绍', trigger: 'blur' }
        ]
      },
      parentLabelOptions: [], // 父级标签下拉框options
      submitLoading: false, // 提交按钮loading
    }
  },
  created () {
    this.initFormData()
    this.loadParentLabelOptions()
  },
  computed: {
    // 当前是编辑页
    isEditPage () {
      return !!this.$route.params?.uuid
    },
    // card标题
    cardTitle () {
      return this.isEditPage? '编辑标签' : '新增标签'
    }
  },
  methods: {
    // 加载父级标签下拉框options
    async loadParentLabelOptions () {
      const res = await labelSearchParentService()
      this.parentLabelOptions = res?.retData ?? []
    },
    // 初始化表单数据
    initFormData () {
      const { name, parentUuid, description, enableState } = this.$route?.params ?? {}
      this.formData = {
        name,
        parentUuid,
        description,
        enableState
      }
    },
    // 提交
    handleSubmit () {
      this.$refs.formRef.validate(valid => {
        const warnMsg = () => {
          this.$message({
            type: 'warning',
            message: '请完善表单信息'
          })
        }
        valid ? this.submitFormData() : warnMsg()
      })
    },
    // 返回
    handleGoBack () {
      this.$router.push({ name: 'categoryLabelManageList' })
    },
    // 提交表单数据
    async submitFormData () {
      const fetchSubmit = this.isEditPage
        ? categoryLabelUpdateService
        : categoryLabelAddService
      const data = {
        ...this.formData,
        uuid: this.$route.params?.uuid
      }
      const loadingFn = state => { this.submitLoading = state }
      const res = await fetchSubmit(data, loadingFn)
      if (isResponseSuccess(res)) {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.handleGoBack()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.w-60 {
  width: 60%;
}
</style>
